namespace ASPNET.StarterKit.IssueTracker
{
    using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;
    using ASPNET.StarterKit.IssueTracker.UserInterfaceLayer;
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class Comments : UserControl, IIssueTab
    {
        [AccessedThroughProperty("btnAdd")]
        private Button _btnAdd;
        [AccessedThroughProperty("grdComments")]
        private DataGrid _grdComments;
        private int _IssueId;
        [AccessedThroughProperty("txtComment")]
        private TextBox _txtComment;
        private object designerPlaceholderDeclaration;

        public Comments()
        {
            base.PreRender += new EventHandler(this.Page_PreRender);
            base.Init += new EventHandler(this.Page_Init);
            this._IssueId = 0;
        }

        public void AddComment(object s, EventArgs e)
        {
            if (this.Page.IsValid)
            {
                new IssueComment(this.IssueId, this.txtComment.Text.Trim(), this.Page.User.Identity.Name).Save();
                this.txtComment.Text = string.Empty;
                this.BindComments();
            }
        }

        public void BindComments()
        {
            this.grdComments.DataSource = IssueComment.GetIssueCommentsByIssueId(this._IssueId);
            this.grdComments.DataBind();
        }

        public void grdCommentsItemDataBound(object s, DataGridItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) | (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                IssueComment currentComment = (IssueComment) e.Item.DataItem;
                Label lblCreatorDisplayName = (Label) e.Item.FindControl("lblCreatorDisplayName");
                lblCreatorDisplayName.Text = currentComment.CreatorDisplayName;
                Label lblDateCreated = (Label) e.Item.FindControl("lblDateCreated");
                lblDateCreated.Text = currentComment.DateCreated.ToString("f");
                Literal ltlComment = (Literal) e.Item.FindControl("ltlComment");
                ltlComment.Text = this.Server.HtmlEncode(currentComment.Comment);
            }
        }

        protected void grdCommentsPage(object s, DataGridPageChangedEventArgs e)
        {
            this.grdComments.CurrentPageIndex = e.NewPageIndex;
            this.BindComments();
        }

        public void Initialize()
        {
            this.BindComments();
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        private void Page_PreRender(object sender, EventArgs e)
        {
            if (this.Page.User.IsInRole("Guest"))
            {
                this.btnAdd.Enabled = false;
            }
        }

        int ASPNET.StarterKit.IssueTracker.UserInterfaceLayer.IIssueTab.IssueId
        {
            get
            {
                return this._IssueId;
            }
            set
            {
                this._IssueId = value;
            }
        }

        protected virtual Button btnAdd
        {
            get
            {
                return this._btnAdd;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click -= new EventHandler(this.AddComment);
                }
                this._btnAdd = value;
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click += new EventHandler(this.AddComment);
                }
            }
        }

        protected virtual DataGrid grdComments
        {
            get
            {
                return this._grdComments;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._grdComments != null)
                {
                    this._grdComments.ItemDataBound -= new DataGridItemEventHandler(this.grdCommentsItemDataBound);
                    this._grdComments.PageIndexChanged -= new DataGridPageChangedEventHandler(this.grdCommentsPage);
                }
                this._grdComments = value;
                if (this._grdComments != null)
                {
                    this._grdComments.ItemDataBound += new DataGridItemEventHandler(this.grdCommentsItemDataBound);
                    this._grdComments.PageIndexChanged += new DataGridPageChangedEventHandler(this.grdCommentsPage);
                }
            }
        }

        public int IssueId
        {
            get
            {
                return this._IssueId;
            }
            set
            {
                this._IssueId = value;
            }
        }

        protected virtual TextBox txtComment
        {
            get
            {
                return this._txtComment;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._txtComment != null)
                {
                }
                this._txtComment = value;
                if (this._txtComment != null)
                {
                }
            }
        }
    }
}

